<?php

/**
 * LoginForm class.
 * LoginForm is the data structure for keeping
 * user login form data. It is used by the 'login' action of 'SiteController'.
 */
class LoginForm extends CFormModel
{

    public $username;
    public $password;
    private $_identity;
    public $device_type;//'ios or android'
    public $device_id;//设备号
    public $verifyCode;

    /**
     * Declares the validation rules.
     * The rules state that username and password are required,
     * and password needs to be authenticated.
     */
    public function rules()
    {
        return array(
            // username and password are required
            array('verifyCode', 'captcha', 'message' => '请输入正确的验证码'),
            array('username, password', 'required'),
            array('username', 'length', 'max' => 100),
            array('password', 'authenticate', 'skipOnError' => true),
        );
    }

    public function behaviors()
    {
        return array(
           'smartCaptcha' => array(
               'class' => 'SmartCaptchaBehavior',
           ),
        );
    }

    /**
     * Declares attribute labels.
     */
    public function attributeLabels()
    {
        return array(
            'password' => '密码',
            'username' => '用户名'
       );
    }

    /**
     * Authenticates the password.
     * This is the 'authenticate' validator as declared in rules().
     */
    public function authenticate($attribute, $params)
    {
        if (!$this->hasErrors())
        {
            $this->_identity = new UserIdentity($this->username, $this->password,false);
            if (!$this->_identity->authenticate())
                $this->addError('username', '用户名或者密码不正确.');
        }
    }

    /**
     * Logs in the user using the given username and password in the model.
     * @return boolean whether login is successful
     */

    /**
     * Logs in the user using the given username and password in the model.
     * @return boolean whether login is successful
     */
    public function login()
    {
        if ($this->_identity === null)
        {
            $this->_identity = new UserIdentity($this->username, $this->password,false);
            $this->_identity->authenticate();
        }
        if ($this->_identity->errorCode === UserIdentity::ERROR_NONE)
        {
            $duration = 0; // 30 days
            Yii::app()->user->login($this->_identity, $duration);
            $this->afterLogin();
            return true;
        }
        else
            return false;
    }

    public function afterLogin()
    {
        $userInfo = Yii::app()->user->getState("info");
        $ip = HttpLib::getRemoteIp();
        $result = array(
            "event"          => 'Login',
            "userId"         => $userInfo['userId'],
            "mobile"         => $userInfo['mobile'],
            "userName"       => $userInfo['userName'],
            "name"           => $userInfo['name'],
            "ip"             => $ip,
            "machine_source" => MOBILEOS==='android'?1:2,
            "login_source"   => 3,
            "timest"         => time(),
        );
        $info = array(
            'device_id' => DEVICEID,
            'user_id'   => $userInfo['userId'],
            'time'      => date('Y-m-d H:i:s'),
            'ip'        => $ip,
            'type'      => MOBILEOS,
            'version'   => APPVERSION,
        );
        $pipe = Yii::app()->cache_php->pipeline();
        $pipe->publish("cot",json_encode($result));
        $pipe->lpush('mobile_login',json_encode($info));
        $pipe->exec();
        return true;
    }

}
